{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 一键获取数据\n",
    "!cd work && bash run/onekey.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 一键训练、量化任务\n",
    "!cd work && bash run/onetasks.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# PPYOLOv2 PPYOLO PPYOLOTiny PicoDet YOLOv3\n",
    "# PPYOLOv2 ['ResNet50_vd_dcn', 'ResNet101_vd_dcn']\n",
    "# PPYOLO ['ResNet50_vd_dcn', 'ResNet18_vd',\n",
    "#     'MobileNetV3_large', 'MobileNetV3_small']\n",
    "# PPYOLOTiny ['MobileNetV3']\n",
    "# PicoDet ['ESNet_s', 'ESNet_m', 'ESNet_l', 'LCNet',\n",
    "#     'MobileNetV3', 'ResNet18_vd']\n",
    "# YOLOv3 ['MobileNetV1', 'MobileNetV1_ssld', 'MobileNetV3',\n",
    "#     'MobileNetV3_ssld', 'DarkNet53', 'ResNet50_vd_dcn', 'ResNet34']\n",
    "# --lr_decay_epochs \"32\" --lr_decay_gamma 1 \\\n",
    "# --save_interval_epochs 4 \\\n",
    "# --pretrain_weights ./output/best_model/model.pdparams \\\n",
    "# 训练 --num_workers 2 \"COCO\" \"IMAGENET\"\n",
    "!cd work && python3 run/train.py \\\n",
    "    --dataset ./dataset/road_fighter_car \\\n",
    "    --epochs 32 \\\n",
    "    --batch_size 1 \\\n",
    "    --learning_rate 0.01 \\\n",
    "    --model PicoDet \\\n",
    "    --backbone ESNet_m \\\n",
    "    --pretrain_weights \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# --save_interval_epochs 4 \\\n",
    "# 裁剪 --model_dir ./output/best_model --save_dir ./output/prune\n",
    "!cd work && python3 run/prune.py \\\n",
    "    --dataset ./dataset/road_fighter_car \\\n",
    "    --epochs 16 \\\n",
    "    --batch_size 1 \\\n",
    "    --learning_rate 0.001 \\\n",
    "    --model_dir ./output/best_model \\\n",
    "    --save_dir ./output/prune \\\n",
    "    --pruned_flops 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# --save_interval_epochs 4 \\\n",
    "# 在线量化 --model_dir ./output/best_model --save_dir ./output/quant\n",
    "!cd work && python3 run/quant.py \\\n",
    "    --dataset ./dataset/road_fighter_car \\\n",
    "    --epochs 16 \\\n",
    "    --batch_size 1 \\\n",
    "    --learning_rate 0.001 \\\n",
    "    --model_dir ./output/best_model \\\n",
    "    --save_dir ./output/quant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 预测\n",
    "!cd work && python3 run/infer.py --model_dir ./output/best_model \\\n",
    "    --predict_image ./dataset/road_fighter_car/JPEGImages/0360.jpg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 查看预测结果图像\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "image_path = \"./work/result/visualize_0360.jpg\"\n",
    "img = Image.open(image_path)\n",
    "plt.imshow(img)\n",
    "plt.show()\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T15:16:33.802623Z",
     "iopub.status.busy": "2022-05-25T15:16:33.801796Z",
     "iopub.status.idle": "2022-05-25T15:16:34.829316Z",
     "shell.execute_reply": "2022-05-25T15:16:34.828256Z",
     "shell.execute_reply.started": "2022-05-25T15:16:33.802580Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 压缩模型\n",
    "!mkdir work/bak\n",
    "!cd work && tar -caf ./bak/PicoDet_ESNet_m_32e_0.01.tar.gz ./output/best_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 清除数据集\n",
    "!rm -r work/dataset/*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 清除模型参数\n",
    "!rm -r work/output/*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 查看支持的模型\n",
    "!cd work && python3 run/train.py --model_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 解压代码\n",
    "!unzip -oq run.zip -d ./work/"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
